package com.bxait.toymail.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.bxait.com.exception.BizCodeEnum;
import com.bxait.com.exception.order.NoStockException;
import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.R;
import com.bxait.com.vo.product.SkuHasStockVo;
import com.bxait.toymail.ware.vo.WareSkuLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bxait.toymail.ware.entity.WareSkuEntity;
import com.bxait.toymail.ware.service.WareSkuService;



/**
 * import org.apache.shiro.authz.annotation.RequiresPermissions;
 * 商品库存
 *
 * @author chen
 * @email 2277411582@qq.com
 * @date 2023-03-13 22:41:32
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * @param params
     * @==============History===============<br/>
     * @Description // TODO  获取商品库存信息
     * @Date 2023/3/25 2023/3/25 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.util.Map<java.lang.String,java.lang.Object>]
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /*
     * @param skuIds
     * @==============History===============<br/>
     * @Description // TODO 查询sku是否有库存
     * @Date 2023/4/13 23:29
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.R
     */
    @PostMapping("/hasStock")
    public R getSkusHasStock(@RequestBody List<Long> skuIds){
        //sku_id，stock
        List<SkuHasStockVo> vos =  wareSkuService.getSkusHasStock(skuIds);
        return R.ok().setData("data",vos);
    }

    /*
     * @param vo
     * @==============History===============<br/>
     * @Description //    锁定库存
     * @Date 2023/5/13 2023/5/13 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return com.bxait.com.utils.R
     */
    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo vo){
        try{
            Boolean stock = wareSkuService.orderLockStock(vo);
            return R.ok();
        }catch (NoStockException e){
            return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(),e.getMessage());
        }
    }

}
